const fs = require('fs');
const path = require('path');

// 读取data.js文件内容
const dataFilePath = path.join(__dirname, 'public', 'js', 'data.js');
const dataContent = fs.readFileSync(dataFilePath, 'utf8');

// 直接提取所有单词的正则表达式
const wordsRegex = /"words":\s*\[(.*?)\]/g;
const allWords = new Set();
let match;

// 提取所有单词
while ((match = wordsRegex.exec(dataContent)) !== null) {
    // 提取单词数组字符串并去除引号
    const wordsStr = match[1].replace(/"/g, '').replace(/\s+/g, '');
    // 分割成单词
    const words = wordsStr.split(',');
    // 添加到集合中
    words.forEach(word => allWords.add(word));
}

// 读取words目录下的所有mp3文件名
const wordsDirPath = path.join(__dirname, 'public', 'voice', 'words');
const audioFiles = fs.readdirSync(wordsDirPath)
    .filter(file => file.endsWith('.mp3'))
    .map(file => file.replace('.mp3', ''));

// 找出缺失的单词
const wordCount = allWords.size;
const missingWords = [...allWords].filter(word => !audioFiles.includes(word.toLowerCase()));

// 输出结果
console.log('总单词数:', wordCount);
console.log('总音频文件数:', audioFiles.length);
console.log('缺失的单词语音资源:');
if (missingWords.length === 0) {
    console.log('没有缺失的单词语音资源');
} else {
    missingWords.forEach(word => console.log(`- ${word}`));
    console.log(`共缺失: ${missingWords.length} 个单词的语音资源`);
}